package m.jclecai.com.fragment.refreshableList;


import android.content.Context;
import android.support.annotation.LayoutRes;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.activity.follow.HotPeopleInfoActivity;
import m.jclecai.com.adapter.base.DefaultViewHolderAdapter;
import m.jclecai.com.adapter.holder.DefaultViewHolder;
import m.jclecai.com.bean.ProofBean;
import m.jclecai.com.bean.Response.ResMyFollowList;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by Dinosa on 2017/11/24.
 * 我定制的东西，这里我是可以删除要给一个条目的；
 */

public class MineOrderListFragment extends RefreshableListFragment {
    @Override
    public void requestData() {
        //这里我们要请求数据的；
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("user", PreferenceUtil.getUserName(getActivity()));
        paramsMap.put("password",PreferenceUtil.getPassword(getActivity()));
        HttpManager.postAsyn(Constant_Url.MY_CUSTOM_ORDER_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                e.printStackTrace();
                mPrListview.onRefreshComplete();
                DisPlay("网络连接异常!!!");
            }

            @Override
            public void onResponse(String response) {
                try {
                    if (!TextUtils.isEmpty(response)) {
                        //这里我们对数据进行要给解析
                        ResMyFollowList resHitList = FastOk.get(response, ResMyFollowList.class);
                        if (resHitList != null) {

                            if("0".equals(resHitList.getStatus())){
                                List<ResMyFollowList.FollowBean> hitList = resHitList.getRecords();
                                int totalPages = Integer.parseInt(resHitList.getPages());

                                mAdapter.addAll(hitList);

                                if (mPage < totalPages) {
                                    mPage += 1;
                                }
                            }else{
                                DisPlay(resHitList.getMessage());

                            }
                        } else {
                            DisPlay("网络连接异常!!!");
                        }
                    } else {
                        DisPlay("网络连接异常!!!");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    DisPlay("网络连接异常!!!");
                }
                mPrListview.onRefreshComplete();
            }
        },paramsMap);
    }

    @Override
    protected DefaultViewHolderAdapter createAdapter() {
        return new MyOrderAdapter(getActivity());
    }



    public class MyOrderAdapter extends DefaultViewHolderAdapter<ResMyFollowList.FollowBean> {


        public MyOrderAdapter(Context context) {
            super(context);
        }

        @Override
        public DefaultViewHolder<ResMyFollowList.FollowBean> getRealViewHolder(Context context) {
            return new FollowViewHolder(context, R.layout.item_list_mine_custom_order);
        }
    }

    public class FollowViewHolder extends DefaultViewHolder<ResMyFollowList.FollowBean> {

        @BindView(R.id.tv_name)
        TextView mTvName;

        @BindView(R.id.tv_content)
        TextView mTvContent;

        @BindView(R.id.tv_muiltiple)
        TextView mTvMuiltiple;

        @BindView(R.id.iv_del)
        ImageView mIvDel;

        public FollowViewHolder(Context context, @LayoutRes int resource) {
            super(context, resource);
        }

        @Override
        public void init(final ResMyFollowList.FollowBean followBean, int position) {
            mTvName.setText(followBean.getUsername());

            String content="<html>单倍:<font color='red'>"+followBean.getBuymoney()+"</font>"+"元-"+
                    "<font color='red'>"+followBean.getMaxmoney()+"</font>元</html>";

            mTvContent.setText(Html.fromHtml(content));
            content="<html>定制:<font color='red'>"+followBean.getBuycount()+"</font>次</html>";
            mTvMuiltiple.setText(Html.fromHtml(content));

            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(HotPeopleInfoActivity.getIntent(getActivity(),followBean.getUser_id()));
                }
            });

            mIvDel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    requestCustomCancelOrder(followBean);
                }
            });
        }



    }

    /**
     * 这里是需要登陆的；
     * 取消定制状态
     */
    private void requestCustomCancelOrder(final ResMyFollowList.FollowBean followBean) {

        final HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("user",PreferenceUtil.getUserName(getActivity()));
        paramsMap.put("password",PreferenceUtil.getPassword(getActivity()));
        paramsMap.put("id",followBean.getUser_id());

        showDialog();
        HttpManager.postAsyn(Constant_Url.CANCEL_HOT_PEOPLE_OREDER, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay("网络异常!!!");
            }

            @Override
            public void onResponse(String response) {
                //这里我们要做一个操作就是判断是否取消成
                if (!TextUtils.isEmpty(response)) {

                    ProofBean proofBean = FastOk.get(response, ProofBean.class);
                    if(proofBean!=null && proofBean.getStatus().equals("0")){
                        DisPlay("取消成功!!!");
                        //将那条进行一个删除；
                        mAdapter.del(followBean);
                    }else{
                        if(proofBean!=null){
                            DisPlay(proofBean.getMessage());
                        }else{
                            DisPlay("网络异常！！！");
                        }
                    }

                }else{
                    DisPlay("网络异常!!!");
                }
                dissDialog();
            }
        },paramsMap);
    }
}
